/**
 * 
 */
package trunk.frontend;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Font;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.GridLayout;
import java.awt.Insets;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;
import java.awt.event.WindowEvent;
import java.awt.event.WindowListener;
import java.io.File;
import java.util.ArrayList;

import javax.swing.BorderFactory;
import javax.swing.Box;
import javax.swing.BoxLayout;
import javax.swing.ButtonGroup;
import javax.swing.JButton;
import javax.swing.JComponent;
import javax.swing.JFileChooser;
import javax.swing.JFormattedTextField;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JRadioButton;
import javax.swing.JScrollPane;
import javax.swing.JTextField;
import javax.swing.KeyStroke;
import javax.swing.border.EtchedBorder;
import javax.swing.border.TitledBorder;
import javax.swing.text.NumberFormatter;

/**
 * @author Lawrence
 *
 */
public class AddDialog extends JFrame implements WindowListener, ActionListener {

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;

	/* width and height*/
	private static final int WIDTH = 380, HEIGHT = 250;	
	
	JButton cancelButton;
	
	JButton submitButton;
	
	ButtonGroup group;
	
	JFormattedTextField textField;
	
	private int cType = 0;
	
	private static final int TYPE_TEXTBOX = 1;
	
	private static final int TYPE_RADIOBUTTON = 4;
	
	private static final int TYPE_LIKERT = 3;
	
	private static final int TYPE_CHECKBOX = 2;
	
	String mainLabel;
	
	int number=0;
	
	JRadioButton checkBox;
	JRadioButton likert;
	JRadioButton radioButton;
	JRadioButton textbox;
	JRadioButton falseButton;
	
	LabelDialog ld;
	
	Likert lik;
	CheckBox cb;
	TextType tt;
	RadioButton rb;
	
	
	
	public AddDialog () {
			
		ld = new LabelDialog();
		
		lik = new Likert();
		cb = new CheckBox();
		tt = new TextType();
		rb = new RadioButton();
		
		
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		initializeContentPane();
	
	}
	

	public void initializeContentPane() {
		
		setLocation(200, 200);
		
		//create label
		JLabel typeOfComponent = new JLabel("What type do you want to create ?");
		JLabel numOfComponent = new JLabel("How many do you want to create?");
		
		
		//label Panel
		JPanel labelPanel = new JPanel(new GridLayout(0, 1));
		labelPanel.add(typeOfComponent);
		labelPanel.add(numOfComponent);
		labelPanel.add(Box.createVerticalStrut(50));
		
		//create button group
		checkBox = new JRadioButton("Check Box");
		checkBox.setActionCommand("Check Box");
		checkBox.addActionListener(this);
		
		likert = new JRadioButton("Likert Scale");
		likert.setActionCommand("Likert Scale");
		likert.addActionListener(this);
		
		radioButton = new JRadioButton("Radio Button");
		radioButton.setActionCommand("Radio Button");
		radioButton.addActionListener(this);
		
		textbox = new JRadioButton("Text Fill");
		textbox.setActionCommand("Text Fill");
		textbox.addActionListener(this);

		falseButton = new JRadioButton("False");
		
		group = new ButtonGroup();
	    group.add(checkBox);
	    group.add(likert);
	    group.add(radioButton);
	    group.add(textbox);
	    group.add(falseButton);

	    JPanel radioPanel = new JPanel(new GridLayout(0, 1));
	    radioPanel.add(checkBox);
	    radioPanel.add(likert);
	    radioPanel.add(radioButton);
	    radioPanel.add(textbox);
		
		//		create field
	    JPanel fieldPanel = new JPanel(new GridLayout(0, 1));
		
	    JLabel tempLabel = new JLabel("   ");
	    textField = new JFormattedTextField(new NumberFormatter());
	    textField.setValue(0);
	    
	    fieldPanel.add(tempLabel);
		fieldPanel.add(textField);
		fieldPanel.add(Box.createVerticalStrut(50));
		
		//field Panel
		JPanel componentPanel = new JPanel(new GridLayout(0, 1));
		componentPanel.add(radioPanel);
		
		componentPanel.add(fieldPanel);
		
		componentPanel.add(Box.createVerticalStrut(50));
		
		//Button Panel
		submitButton = new JButton("Submit");		
		submitButton.addActionListener(this);		
		submitButton.setMnemonic(KeyEvent.VK_S);		
		JPanel submitPanel = new JPanel();
		submitPanel.add(submitButton);
		
		cancelButton = new JButton("Cancel");
		cancelButton.setMnemonic(KeyEvent.VK_C);
		cancelButton.addActionListener(this);
		JPanel cancelPanel = new JPanel();
		cancelPanel.add(cancelButton);
		
		JPanel buttonPanel = new JPanel();
		buttonPanel.setLayout
		(new BoxLayout(buttonPanel, BoxLayout.X_AXIS));
		buttonPanel.add(Box.createRigidArea(new Dimension(0,3)));
		
		buttonPanel.add(submitPanel);
		buttonPanel.add(cancelPanel);
			
		
		//sub panel
			
		JPanel subPanel = new JPanel(new BorderLayout());
		subPanel.add(labelPanel, BorderLayout.CENTER);
		subPanel.add(componentPanel, BorderLayout.EAST);		
		
		
		
		JPanel mainPanel = new JPanel(new BorderLayout());		
		mainPanel.setPreferredSize(new Dimension(WIDTH, HEIGHT));		
		mainPanel.add(subPanel, BorderLayout.CENTER);      
		mainPanel.add(buttonPanel, BorderLayout.SOUTH); 
		
		mainPanel.setBorder(
				new TitledBorder(new EtchedBorder(),
						"Add a Section",
						TitledBorder.DEFAULT_JUSTIFICATION
						, TitledBorder.CENTER,
						new Font("Helvetica", Font.BOLD, 17)
						, new Color(0, 0, 250)));
		
		getContentPane().add(mainPanel);
		pack();
	}
	

	public void windowActivated(WindowEvent arg0) {
		// TODO Auto-generated method stub
		
	}


	public void windowClosed(WindowEvent arg0) {
		// TODO Auto-generated method stub
		
	}


	public void windowClosing(WindowEvent arg0) {
		// TODO Auto-generated method stub
		
	}


	public void windowDeactivated(WindowEvent arg0) {
		// TODO Auto-generated method stub
		
	}


	public void windowDeiconified(WindowEvent arg0) {
		// TODO Auto-generated method stub
		
	}


	public void windowIconified(WindowEvent arg0) {
		// TODO Auto-generated method stub
		
	}


	public void windowOpened(WindowEvent arg0) {
		// TODO Auto-generated method stub
		
	}


	@SuppressWarnings("deprecation")
	public void actionPerformed(ActionEvent e) {
		
		if(e.getActionCommand() == "Text Fill") {
			
			textField.disable();
			
			System.out.println(e.getActionCommand());
			this.cType = this.TYPE_TEXTBOX;
			//TODO 
		}
		
		if(e.getActionCommand() == "Likert Scale") {
			
			textField.disable();
			
			System.out.println(e.getActionCommand());
			this.cType = this.TYPE_LIKERT;
			//TODO 
		}
		
		if(e.getActionCommand() == "Check Box") {
			
			textField.enable();
			
			System.out.println(e.getActionCommand());
			this.cType = this.TYPE_CHECKBOX;
			
			//TODO 
		}
		
		if(e.getActionCommand() == "Radio Button") {
			
			textField.enable();
			
			System.out.println(e.getActionCommand());
			this.cType = this.TYPE_RADIOBUTTON;
			//TODO 
		}
		
		if (e.getSource() == submitButton) {
					
			
			textField.enable();
			
			System.out.println(this.textField.getValue());
			
			number = ((Number) this.textField.getValue()).intValue();
			
			System.out.println(number);
			
			textField.setText("");
						
			falseButton.setSelected(true);
			
			ld.setAd(this);
			
			if( cType == this.TYPE_CHECKBOX ) {
				ld.setVisible(true);
				cb = new CheckBox();
				
				ld.setNumber(number);
				
			}
			
			if( cType == this.TYPE_LIKERT ) {
				ld.setVisible(true);
				lik = new Likert();
				
				ld.setNumber(number);
			}
			
			if( cType == this.TYPE_RADIOBUTTON ) {
				ld.setVisible(true);
				rb = new RadioButton();
				
				ld.setNumber(number);
			}
			
			if( cType == this.TYPE_TEXTBOX ) {
				ld.setVisible(true);
				tt = new TextType();
				
				ld.setNumber(number);
			}
			
			setVisible(false);
			
			
			//TODO
		}
		if (e.getSource() == cancelButton) {
			setVisible(false);
		}
	}

	public void setcbMainLabel(String ml, int number) {
		
		cb.setLabel(ml, number);
		
	}
	
	public void setrbMainLabel(String ml, int number) {
		
		rb.setLabel(ml, number);
		
	}
	
	public void setttMainLabel(String ml, int number) {
		
		tt.setLabel(ml, number);
		
	}
	
	public void setlikMainLabel(String ml, int number) {
		
		lik.setLabel(ml, number);
		
	}
	
	
	public static void main(String[] args) {
		AddDialog frame = new AddDialog();
		
		frame.setVisible(true);
	}
	
	
}
